<?php

namespace Proj\ResmanBundle\Helper;

class GenerationHelper {

    static function generateScheduleFrom($contents) {
        $dom_document = new \DOMDocument();
        $dom_document->loadHTML($contents);

        // using DOMXpath to navigate through html via DOM
        $dom_xpath = new \DOMXPath($dom_document);

        // Xpath expression
        $expression = "/html/body/center/table/tr";

        // get the table content
        $table_content = $dom_xpath->query($expression);

        $result = array();
        $index = 0;
        foreach ($table_content as $table_row) {
            if ($index > 0) {
                $temp_row = array();
                $table_row_contents = $table_row->getElementsByTagName("td");
                $i = 0;
                foreach ($table_row_contents as $content) {
                    switch ($i++) {
                        case 0:$temp_row["ziua"] = $content->nodeValue;
                            break;
                        case 1:$temp_row["ore"] = $content->nodeValue;
                            break;
                        case 2:$temp_row["frecventa"] = $content->nodeValue;
                            break;
                        case 3:$temp_row["sala"] = $content->nodeValue;
                            break;
                        case 4:$temp_row["an"] = $content->nodeValue;
                            break;
                        case 5:$temp_row["formatia"] = $content->nodeValue;
                            break;
                        case 6:$temp_row["tip"] = $content->nodeValue;
                            break;
                        case 7:$temp_row["disciplina"] = $content->nodeValue;
                            break;
                    }
                }
                $result[$index++] = $temp_row;
            } else { $index++;}
        }

        return $result;
    }

}

?>
